package com.bankapp.domain.bank;

import com.bankapp.exception.bank.BankException;
import com.bankapp.exception.bank.OverDraftLimitExceededException;

public class CheckingAccount extends AbstractAccount {
    double overdraft;
    double overdraftNow;

    public CheckingAccount(double overdraft) {
        this.overdraft = overdraft;
        this.overdraftNow = overdraft;
    }

    @Override
    public void withdraw(double amount) throws BankException {
        if (this.getBalance() + overdraft < amount) {
            throw new OverDraftLimitExceededException(this, this.getBalance() + overdraft, "");
        } else {
            if (getBalance() < amount) {
                overdraftNow = overdraft + getBalance() - amount;
            }
            super.withdraw(amount);
        }
    }

    @Override
    public void deposit(double amount) throws BankException {
        super.deposit(amount);
        if (getBalance() < 0) {
            overdraftNow = overdraftNow + amount;
        } else {
            overdraftNow = overdraft;
        }

    }

    @Override
    public double maximumAmountToWithDraw() {
        return super.maximumAmountToWithDraw() + overdraftNow;
    }
}
